﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Windows.Controls.Primitives;

using ICSharpCode.Core.Presentation;
using ICSharpCode.Core.Services;
using ICSharpCode.Core;
namespace AddInMain
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        log4net.ILog logger = log4net.LogManager.GetLogger(typeof(App));
        const string mainMenuPath = "/SharpDevelop/Workbench/MainMenu";
        ToolBar[] toolBars;
        StatusBar statusBar = new StatusBar();
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                StartupSettings startup = new StartupSettings();
                startup.ApplicationRootPath = AppDomain.CurrentDomain.BaseDirectory;
                startup.AllowUserAddIns = true;
                startup.AllowAddInConfigurationAndExternalAddIns = true;

                startup.AddAddInsFromDirectory(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AddIns"));

                CoreStartup coreSt = new CoreStartup("ADDIn Main");
                
                if (startup.AllowAddInConfigurationAndExternalAddIns)
                {
                    coreSt.ConfigureExternalAddIns(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AddIns.xml"));
                }
                if (startup.AllowUserAddIns)
                {
                    coreSt.ConfigureUserAddIns(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AddInInstallTemp"),
                                                System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AddIns"));
                }

              
                mainMenu.ItemsSource = MenuService.CreateMenuItems(this, this, mainMenuPath, activationMethod: "MainMenu", immediatelyExpandMenuBuildersForShortcuts: true);

                toolBars = ToolBarService.CreateToolBars(this, this, "/SharpDevelop/Workbench/ToolBar");
                if (toolBars != null)
                {
                    foreach (ToolBar tb in toolBars)
                    {
                        DockPanel.SetDock(tb, Dock.Top);
                        dockPanel.Children.Insert(1, tb);
                    }
                    DockPanel.SetDock(statusBar, Dock.Bottom);
                    dockPanel.Children.Insert(dockPanel.Children.Count - 2, statusBar);
                }
                UpdateMenu();

            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }
        }

        void UpdateMenu()
        {
            MenuService.UpdateStatus(mainMenu.ItemsSource);
            foreach (ToolBar tb in toolBars)
            {
                ToolBarService.UpdateStatus(tb.ItemsSource);
            }
        }
    }
}
